package com.muyu.danmo.service.impl;

import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.DateUtils;
import com.muyu.danmo.domain.DanmoAddresses;
import com.muyu.danmo.mapper.DanmoAddressesMapper;
import com.muyu.danmo.service.IDanmoAddressesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户收货地址Service业务层处理
 *
 * @author yangle
 * @date 2024-10-05
 */
@Service
public class DanmoAddressesServiceImpl implements IDanmoAddressesService {
    @Autowired
    private DanmoAddressesMapper danmoAddressesMapper;

    /**
     * 查询用户收货地址
     *
     * @param id 用户收货地址主键
     * @return 用户收货地址
     */
    @Override
    public DanmoAddresses selectDanmoAddressesById(Long id) {
        return danmoAddressesMapper.selectDanmoAddressesById(id);
    }

    /**
     * 查询用户收货地址列表
     *
     * @param danmoAddresses 用户收货地址
     * @return 用户收货地址
     */
    @Override
    public List<DanmoAddresses> selectDanmoAddressesList(DanmoAddresses danmoAddresses) {
//        Long userId = SecurityUtils.getUserId();
//        danmoAddresses.setUserId(userId);
        return danmoAddressesMapper.selectDanmoAddressesList(danmoAddresses);
    }

    /**
     * 新增用户收货地址
     *
     * @param danmoAddresses 用户收货地址
     * @return 结果
     */
    @Override
    public Result insertDanmoAddresses(DanmoAddresses danmoAddresses) {
        if (danmoAddresses.getName() ==null || danmoAddresses.getName().equals("")){
            return Result.error("请输入收货人姓名");
        }
        if (danmoAddresses.getPhone()==null || danmoAddresses.getPhone().equals("")){
            return Result.error("请输入手机号");
        }
        if (danmoAddresses.getProvince()==null || danmoAddresses.getProvince().equals("")){
            return Result.error("请选择省份");
        }
        if (danmoAddresses.getCity()==null || danmoAddresses.getCity().equals("")){
            return Result.error("请选择市");
        }
        if (danmoAddresses.getRegion()==null || danmoAddresses.getRegion().equals("")){
            return Result.error("请选择县");
        }
        if (danmoAddresses.getAddressDetail()==null || danmoAddresses.getAddressDetail().equals("")){
            return Result.error("请输入详细地址");
        }
        if (danmoAddresses.getPhone()==null || danmoAddresses.getPhone().equals("") || danmoAddresses.getPhone().length()!=11 || !danmoAddresses.getPhone().matches("^[1][3-9][0-9]{9}$")){
            return Result.error("请输入正确手机号");
        }

//        String regex = "^(?:(?:\\+|00)86)?1(?:(?:3[\\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\\d])|(?:9[189]))\\d{8}$";
//        Pattern pattern = Pattern.compile(regex);
//        Matcher matcher = pattern.matcher(danmoAddresses.getPhone());
//        if (!matcher.matches()){
//            return Result.error("手机号格式不正确");
//        }
//        try {
//            if (danmoAddresses.getIsDefault()==1 ){
//                DanmoAddresses danmoAddresses1 = new DanmoAddresses();
//                danmoAddresses1.setIsDefault(1);
//                List<DanmoAddresses> danmoAddresses2 = danmoAddressesMapper.selectDanmoAddressesList(danmoAddresses1);
//                 if (danmoAddresses2!=null){
//                     for (DanmoAddresses addresses : danmoAddresses2) {
//                         addresses.setIsDefault(0);
//                         danmoAddressesMapper.updateDanmoAddresses(addresses);
//                     }
//                 }
//
//            }
//            if (danmoAddresses.getIsDefault() ==0){
                danmoAddresses.setCreateTime(DateUtils.getNowDate());
//                Long userId = SecurityUtils.getUserId();
//                danmoAddresses.setUserId(userId);
        if(null == danmoAddresses.getId()){
            danmoAddressesMapper.insertDanmoAddresses(danmoAddresses);
        }else {
            danmoAddressesMapper.updateById(danmoAddresses);
        }

//                updateDanmoAddresses(danmoAddresses);
//            }

            return Result.success(danmoAddresses,"添加成功");
//        }catch (Exception e){
//            return Result.error("添加失败");
//        }


    }

    /**
     * 修改用户收货地址
     *
     * @param danmoAddresses 用户收货地址
     * @return 结果
     */
    @Override
    public Result updateDanmoAddresses(DanmoAddresses danmoAddresses) {

        if (danmoAddresses.getIsDefault()==0){
            DanmoAddresses danmoAddresses1 = new DanmoAddresses();
            danmoAddresses1.setUserId(danmoAddresses.getUserId());
            danmoAddresses1.setIsDefault(0);
            DanmoAddresses danmoAddresses2= danmoAddressesMapper.selectDanmoAddresses(danmoAddresses1);
            if (danmoAddresses2!=null){
                danmoAddresses2.setIsDefault(1);
                danmoAddressesMapper.updateDanmoAddresses(danmoAddresses2);
            }
        }

//            if (danmoAddresses.getIsDefault()==null || danmoAddresses.getIsDefault().equals("")){
//                if (danmoAddresses.getName() ==null || danmoAddresses.getName().equals("")){
//                    return Result.error("请输入收货人姓名");
//                }
//                if (danmoAddresses.getPhone()==null || danmoAddresses.getPhone().equals("")){
//                    return Result.error("请输入手机号");
//                }
//                if (danmoAddresses.getProvince()==null || danmoAddresses.getProvince().equals("")){
//                    return Result.error("请选择省份");
//                }
//                if (danmoAddresses.getCity()==null || danmoAddresses.getCity().equals("")){
//                    return Result.error("请选择市");
//                }
//                if (danmoAddresses.getRegion()==null || danmoAddresses.getRegion().equals("")){
//                    return Result.error("请选择县");
//                }
//                if (danmoAddresses.getAddressDetail()==null || danmoAddresses.getAddressDetail().equals("")){
//                    return Result.error("请输入详细地址");
//                }
//            }



//            if (danmoAddresses.getIsDefault()==null || danmoAddresses.getIsDefault().equals("")){
//                return Result.error("请选择是否默认地址");
//            }
//            String regex = "^(?:(?:\\+|00)86)?1(?:(?:3[\\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\\d])|(?:9[189]))\\d{8}$";
//            Pattern pattern = Pattern.compile(regex);
//            Matcher matcher = pattern.matcher(danmoAddresses.getPhone());
//            if (!matcher.matches()){
//                return Result.error("手机号格式不正确");
//            }
//            if (danmoAddresses.getPhone()==null || danmoAddresses.getPhone().equals("") || danmoAddresses.getPhone().length()!=11 || !danmoAddresses.getPhone().matches("^[1][3-9][0-9]{9}$")){
//                return Result.error("请输入正确手机号");
//            }
            danmoAddresses.setUpdateTime(DateUtils.getNowDate());
//            if (danmoAddresses.getIsDefault()==1 ){
//                DanmoAddresses danmoAddresses1 = new DanmoAddresses();
//                danmoAddresses1.setIsDefault(1);
//                List<DanmoAddresses> danmoAddresses2 = danmoAddressesMapper.selectDanmoAddressesList(danmoAddresses1);
//                if (danmoAddresses2!=null){
//                    for (DanmoAddresses addresses : danmoAddresses2) {
//                        addresses.setIsDefault(0);
//                        danmoAddressesMapper.updateDanmoAddresses(addresses);
//                    }
//                }
//
//            }
            danmoAddressesMapper.updateDanmoAddresses(danmoAddresses);
            return Result.success(danmoAddresses,"修改成功");




    }

    /**
     * 批量删除用户收货地址
     *
     * @param ids 需要删除的用户收货地址主键
     * @return 结果
     */
    @Override
    public Result deleteDanmoAddressesByIds(Long[] ids) {

        List<DanmoAddresses> address = danmoAddressesMapper.selectDanmoAddressesByIds(ids);
        System.out.println("aaa"+address);
        danmoAddressesMapper.deleteDanmoAddressesByIds(ids);
        return Result.success(1,"删除成功");
    }

    /**
     * 删除用户收货地址信息
     *
     * @param id 用户收货地址主键
     * @return 结果
     */
    @Override
    public Result deleteDanmoAddressesById(Long id) {
        danmoAddressesMapper.deleteDanmoAddressesById(id);
        return Result.success(id,"删除成功");
    }
}
